#!/bin/bash
###############################################################################
#
# Title: Post Script for Salt Installation
# Authors: Shane Lee
# Date: December 2015
#
# Description: This script copies the minion config file and starts the salt
#              service
#
# Requirements:
#    - None
#
# Usage:
#     This script is run as a part of the macOS Salt Installation
#
###############################################################################

###############################################################################
# Define Variables
###############################################################################
# Get Minor Version
OSX_VERSION=$(sw_vers | grep ProductVersion | cut -f 2 -d: | tr -d '[:space:]')
MINOR=$(echo ${OSX_VERSION} | cut -f 2 -d.)
# Path Variables
INSTALL_DIR="/opt/salt"
BIN_DIR="$INSTALL_DIR/bin"
CONFIG_DIR="/etc/salt"
TEMP_DIR="/tmp"
SBIN_DIR="/usr/local/sbin"

###############################################################################
# Set up logging and error handling
###############################################################################
echo "Post install script started on:" > "$TEMP_DIR/postinstall.txt"
date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/postinstall.txt"
trap 'quit_on_error $LINENO $BASH_COMMAND' ERR

quit_on_error() {
    echo "$(basename $0) caught error on line : $1 command was: $2" >> "$TEMP_DIR/postinstall.txt"
    exit -1
}

###############################################################################
# Check for existing minion config, copy if it doesn't exist
###############################################################################
if [ ! -f "$CONFIG_DIR/minion" ]; then
    echo "Config: Copy Started..." >> "$TEMP_DIR/postinstall.txt"
    cp "$CONFIG_DIR/minion.dist" "$CONFIG_DIR/minion"
    echo "Config: Copied Successfully" >> "$TEMP_DIR/postinstall.txt"
fi

###############################################################################
# Create symlink to salt-config.sh
###############################################################################
if [ ! -d "$SBIN_DIR" ]; then
    echo "Symlink: Creating $SBIN_DIR..." >> "$TEMP_DIR/postinstall.txt"
    mkdir "$SBIN_DIR"
    echo "Symlink: Created Successfully" >> "$TEMP_DIR/postinstall.txt"
fi
echo "Symlink: Creating symlink for salt-config..." >> "$TEMP_DIR/postinstall.txt"
ln -sf "$BIN_DIR/salt-config.sh" "$SBIN_DIR/salt-config"
echo "Symlink: Created Successfully" >> "$TEMP_DIR/postinstall.txt"

###############################################################################
# Add salt to paths.d
###############################################################################
if [ ! -d "/etc/paths.d" ]; then
    echo "Path: Creating paths.d directory..." >> "$TEMP_DIR/postinstall.txt"
    mkdir /etc/paths.d
    echo "Path: Created Successfully" >> "$TEMP_DIR/postinstall.txt"
fi
echo "Path: Adding salt to the path..." >> "$TEMP_DIR/postinstall.txt"
sh -c "echo \"$BIN_DIR\" > /etc/paths.d/salt"
sh -c "echo \"$SBIN_DIR\" >> /etc/paths.d/salt"
echo "Path: Added Successfully" >> "$TEMP_DIR/postinstall.txt"

###############################################################################
# Register Salt as a service
###############################################################################
setup_services_maverick() {
    echo "Service: Using old (< 10.10) launchctl interface" >> "$TEMP_DIR/postinstall.txt"
    if /bin/launchctl list "com.saltstack.salt.minion" &> /dev/null; then
        echo "Service: Stopping salt-minion..." >> "$TEMP_DIR/postinstall.txt"
        launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.minion.plist
        echo "Service: Stopped Successfully" >> "$TEMP_DIR/postinstall.txt"
    fi;
    echo "Service: Starting salt-minion..." >> "$TEMP_DIR/postinstall.txt"
    launchctl load -w /Library/LaunchDaemons/com.saltstack.salt.minion.plist || return 1
    echo "Service: Started Successfully" >> "$TEMP_DIR/postinstall.txt"

    echo "Service: Disabling Master, Syndic, and API services..." >> "$TEMP_DIR/postinstall.txt"
    launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.api.plist
    launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.master.plist
    launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.syndic.plist
    echo "Service: Disabled Successfully" >> "$TEMP_DIR/postinstall.txt"

    return 0
}

setup_services_yosemite_and_later() {
    echo "Service: Using new (>= 10.10) launchctl interface" >> "$TEMP_DIR/postinstall.txt"
    echo "Service: Enabling salt-minion..." >> "$TEMP_DIR/postinstall.txt"
    launchctl enable system/com.saltstack.salt.minion
    echo "Service: Enabled Successfully" >> "$TEMP_DIR/postinstall.txt"

    echo "Service: Bootstrapping salt-minion..." >> "$TEMP_DIR/postinstall.txt"
    launchctl bootstrap system /Library/LaunchDaemons/com.saltstack.salt.minion.plist
    echo "Service: Bootstrapped Successfully" >> "$TEMP_DIR/postinstall.txt"

    if /bin/launchctl list "com.saltstack.salt.minion" &> /dev/null; then
        echo "Service: Service Running" >> "$TEMP_DIR/postinstall.txt"
    else
        echo "Service: Kickstarting Service..." >> "$TEMP_DIR/postinstall.txt"
        launchctl kickstart -kp system/com.saltstack.salt.minion
        echo "Service: Kickstarted Successfully" >> "$TEMP_DIR/postinstall.txt"
    fi

    echo "Service: Started Successfully" >> "$TEMP_DIR/postinstall.txt"

    echo "Service: Disabling Master, Syndic, and API services" >> "$TEMP_DIR/postinstall.txt"
    launchctl disable system/com.saltstack.salt.master
    launchctl disable system/com.saltstack.salt.syndic
    launchctl disable system/com.saltstack.salt.api
    echo "Service: Disabled Successfully" >> "$TEMP_DIR/postinstall.txt"

    return 0
}

echo "Service: Configuring..." >> "$TEMP_DIR/postinstall.txt"
case $MINOR in
        9 )
                setup_services_maverick;
                ;;
        * )
                setup_services_yosemite_and_later;
                ;;
esac
echo "Service: Configured Successfully" >> "$TEMP_DIR/postinstall.txt"

echo "Post install completed successfully on:" >> "$TEMP_DIR/postinstall.txt"
date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/postinstall.txt"

exit 0
